package com.itheima.reggin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggin.common.CustomException;
import com.itheima.reggin.entity.Category;
import com.itheima.reggin.entity.Dish;
import com.itheima.reggin.mapper.CategoryMapper;
import com.itheima.reggin.service.CategoryService;
import com.itheima.reggin.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService{
    @Autowired
    private DishService dishService;

    /**
     * 根据i的删除分类，删除之前需要进行判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        //条件构造器
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper =  new LambdaQueryWrapper<>();
        //根据id进行查询菜品数据
        dishLambdaQueryWrapper.eq(Dish::getId,id);
        int count1 = dishService.count(dishLambdaQueryWrapper);
        //如果已经关联
        if(count1 > 0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }

        super.removeById(id);

    }
}
